package com.cx.interceptor;

import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cx.service.admin.basic.LoginManager;
import org.cx.entity.PageData;
import org.cx.util.FastJsonUtils;
import org.cx.util.LogInfoUtil;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.ModelAndView;

import com.cx.service.RedisService;

public class AdminInterceptor implements HandlerInterceptor {
	
	@Resource
	private RedisService redisService;
	@Resource
	private LoginManager loginService;
	
    //在请求处理之前进行调用（Controller方法调用之前
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
    	
    	if ("OPTIONS".equals(request.getMethod())) {
    		return true; 
		}
    	
    	//获取请求路径
    	String path = request.getServletPath();
    	if( path.equalsIgnoreCase("/user/login")
    		|| path.equalsIgnoreCase("/admin/basic/login/login")
    		|| path.indexOf("/admin/basic/upload/getTemplate/") > -1
    		|| path.indexOf("/admin/basic/readfile/") > -1
    		|| path.equalsIgnoreCase("/admin/basic/outside/authorizationTemp")
			|| path.equalsIgnoreCase("/admin/basic/login/verifyCode")
    		|| path.equalsIgnoreCase("/h5/activity/selectAllList")||path.equalsIgnoreCase("/admin/basic/member/exportMemberDetail")
			|| path.equalsIgnoreCase("/admin/basic/org/exportOrgDetail")
			|| path.equalsIgnoreCase("/admin/basic/user/exportUserDetail")
				|| path.equalsIgnoreCase("/admin/basic/user/exportClerkDetail")
                || path.equalsIgnoreCase("/admin/basic/login/sendSMSForPwd")
                || path.equalsIgnoreCase("/admin/basic/login/updateForgotPassword")
				|| path.equalsIgnoreCase("/admin/basic/outside/outsideRegister")
				|| path.equalsIgnoreCase("/admin/basic/outside/sendSMSForCode")
			){
    		
    		return true; 
    	}

    	//获取token
    	String token = request.getHeader("token");
    	if(!redisService.exists(token)){
    		response.setCharacterEncoding("utf-8");
    		response.setContentType("text/html;charset=utf-8");
    		PrintWriter printWriter = response.getWriter();    
            printWriter.write("{\"status\":\"-99\",\"errorMsg\":\"无效请求\",\"body\":\"\"}");    
            return false; 
    	}else{
    		StringBuffer sb = new StringBuffer();
        	Enumeration em = request.getParameterNames();
        	 while (em.hasMoreElements()) {
        		 String name = (String) em.nextElement();
        		 sb.append("["+name+":"+request.getParameter(name)+"]");
        	}
        	LogInfoUtil.info(H5Interceptor.class, "调度："+path, "传参："+sb.toString());
    		//延长token有效时间
    		redisService.setExpireTime(token, 60 * 60L);
    	}
        
    	return true; 
    }
    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        //System.out.println("postHandle被调用");
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        //System.out.println("afterCompletion被调用");
    }
}
